import { View, Text, Button, Input } from '@tarojs/components'
import Taro from '@tarojs/taro'
import { useState } from 'react'
import { useAppContext } from '../../../store'
import { DataManager } from '../../../utils'
import './index.scss'

export default function ChefPoints() {
  const { state, dispatch } = useAppContext()
  const [selectedUser, setSelectedUser] = useState('')
  const [pointsToAdd, setPointsToAdd] = useState('')

  // 这里简化逻辑，只处理当前用户
  const currentUser = DataManager.getUser()

  const handleAddPoints = () => {
    if (!currentUser || !pointsToAdd || parseInt(pointsToAdd) <= 0) {
      Taro.showToast({ title: '请输入积分', icon: 'error' })
      return
    }

    const updatedUser = { ...currentUser, points: currentUser.points + parseInt(pointsToAdd) }
    // 更新用户数据
    DataManager.saveUser(updatedUser)
    
    // 更新状态
    dispatch({ type: 'SET_USER', payload: updatedUser })
    
    Taro.showToast({ title: '积分添加成功', icon: 'success' })
    setPointsToAdd('')
  }

  const handleBack = () => {
    Taro.navigateBack()
  }

  return (
    <View className='chef-points'>
      <View className='header'>
        <View className='header-left'>
          <Button className='back-btn' onClick={handleBack}>← 返回</Button>
          <Text className='title'>积分管理</Text>
        </View>
      </View>

      <View className='points-form'>
        <View className='form-item'>
          <Text className='label'>当前用户</Text>
          <View className='user-info'>
            <Text className='user-name'>{currentUser?.name || '未登录'}</Text>
            <Text className='user-points'>当前积分: {currentUser?.points || 0}</Text>
          </View>
        </View>

        <View className='form-item'>
          <Text className='label'>添加积分</Text>
          <Input
            className='input'
            type='number'
            value={pointsToAdd}
            onInput={(e) => setPointsToAdd(e.detail.value)}
            placeholder='请输入要添加的积分'
          />
        </View>

        <Button className='add-points-btn' onClick={handleAddPoints}>
          添加积分
        </Button>
      </View>
    </View>
  )
} 